<template>
  <div class="mod-user">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.name"
          placeholder="名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="dataForm.phone"
          placeholder="手机号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="dataForm.number"
          placeholder="联系电话"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="dataForm.remark"
          placeholder="备注"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <div class="block"></div>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="resetDataList()">重置</el-button>
        <el-button
          v-if="isAuth('sys:user:save')"
          type="primary"
          @click="addOrUpdateHandle()"
          >新增</el-button
        >
        <el-button
          v-if="isAuth('sys:user:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="supplier"
        header-align="center"
        align="center"
        width="80"
        label="名称"
      >
      </el-table-column>
      <el-table-column
        prop="contacts"
        header-align="center"
        align="center"
        label="联系人"
      >
      </el-table-column>
      <el-table-column
        prop="telephone"
        header-align="center"
        align="center"
        label="手机号码"
      >
      </el-table-column>
      <el-table-column
        prop="email"
        header-align="center"
        align="center"
        width="180"
        label="电子邮箱"
      >
      </el-table-column>
      <el-table-column
        prop="phoneNum"
        header-align="center"
        align="center"
        label="联系电话"
      >
      </el-table-column>
      <el-table-column
        prop="fax"
        header-align="center"
        align="center"
        label="传真"
      >
      </el-table-column>
      <el-table-column
        prop="advanceIn"
        header-align="center"
        align="center"
        label="预付款"
      >
      </el-table-column>
      <el-table-column
        prop="beginNeedGet"
        header-align="center"
        align="center"
        label="初期应收"
      >
      </el-table-column>
      <el-table-column
        prop="beginNeedPay"
        header-align="center"
        align="center"
        label="初期应付"
      >
      </el-table-column>
      <el-table-column
        prop="allNeedGet"
        header-align="center"
        align="center"
        label="末期应收"
      >
      </el-table-column>
      <el-table-column
        prop="allNeedPay"
        header-align="center"
        align="center"
        label="末期应付"
      >
      </el-table-column>
      <el-table-column
        prop="taxRate"
        header-align="center"
        align="center"
        label="税率"
      >
      </el-table-column>
      <el-table-column
        prop="enabled"
        header-align="center"
        align="center"
        label="状态"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enabled"
            active-color=" #0f63b8"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
            @change="updateMemberStatus(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('sys:user:update')"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.$index, scope.row.id, pageIndex, pageSize,dataForm.name,dataForm.phone,dataForm.number,dataForm.remark)"
            >修改</el-button
          >
          <el-button
            v-if="isAuth('sys:user:delete')"
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./customer-add-or-update";
export default {
  data() {
    return {
      dataForm: {
       number: "",
        phone: "",
        name: "",
        remark: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let type = "客户";
      let str = `{"supplier":"${this.dataForm.name}","type":"${type}","phonenum":"${this.dataForm.number}","telephone":"${this.dataForm.phone}","description":"${this.dataForm.remark}"}`;
      var search = encodeURIComponent(str);
      this.$http({
        url: this.$http.adornUrl(`/supplier/list?search=${search}`),
        method: "get",
        params:this.$http.adornParams({
          currentPage: this.pageIndex,
          pageSize: this.pageSize,
        }),
      }).then(({ data }) => {
        //  console.log(data)
        if (data && data.code === 0) {
          this.dataList = data.data;
          this.totalPage = data.data.length;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 更新状态
    updateMemberStatus(data) {
      console.log("最新信息", data);
      let { id, status } = data;
      //发送请求修改状态
      this.$http({
        url: this.$http.adornUrl("/supplier/batchSetEnable"),
        method: "post",
        data: this.$http.adornData(
          {
            enabled: data.enabled,
            supplierIDs: data.id,
          },
          false
        ),
      }).then(({ data }) => {
        console.log(data);
        if (data.code == 0) {
          this.$message({
            type: "success",
            message: "状态更新成功",
          });
        }
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(index, id, pageIndex, pageSize,name,phone,number,remark) {
      // console.log(index, id, pageIndex, pageSize,name,phone,number,remark)
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(index, id, pageIndex, pageSize,name,phone,number,remark);
      });
    },
    // 删除
    deleteHandle(id) {
      console.log(id)
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
          console.log(ids)
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/supplier/batchDeleteSupplierByIds"),
           method: "get",
            params: this.$http.adornParams({
              ids:ids.toString(),
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else if (data.code === 601) {
              this.$confirm(data.msg, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
              })
                .then(() => {
                  console.log(1111);
                  this.$http({
                    url: this.$http.adornUrl(`/supplier/batchDeleteSupplierByIds`),
                    method: "get",
                    params: this.$http.adornParams({
                      ids: ids.toString(),
                      deleteType: 2,
                    }),
                  }).then(({ data }) => {
                    if (data.code == 0) {
                      this.$message({
                        type: "success",
                        message: "删除成功!",
                      });
                       this.getDataList();
                    }
                  });
                })
                .catch(() => {
                  this.$message({
                    type: "info",
                    message: "已取消删除",
                  });
                });
            }else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },

    // 重置
    resetDataList() {
      // console.log(11)
      this.dataForm.name = "";
      this.dataForm.phone = "";
      this.dataForm.number = "";
      this.dataForm.remark = "";
      this.getDataList();
    },
  },
};
</script>